package com.eagle.epay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eagle.epay.entity.Product;
import com.eagle.epay.entity.Shipping;
import com.eagle.epay.exception.ServiceException;
import com.eagle.epay.form.ShippingForm;
import com.eagle.epay.mapper.ShippingMapper;
import com.eagle.epay.service.IShippingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author eagle
 * @since 2019-12-21
 */
@Service
public class ShippingServiceImpl extends ServiceImpl<ShippingMapper, Shipping> implements IShippingService {

	@Override
	public Integer add(Integer uid, ShippingForm form) {
		Shipping entity = new Shipping();
		BeanUtils.copyProperties(form, entity);
		entity.setUserId(uid);

		boolean result = this.save(entity);
		if(result){
			return entity.getId();
		}
		throw new ServiceException("save shipping error");
	}

	@Override
	public void delete(Integer uid, Integer shippingId) {
		QueryWrapper<Shipping> wrapper = new QueryWrapper();
		wrapper.in("id",shippingId);
		wrapper.in("user_id",uid);
		this.remove(wrapper);
	}

	@Override
	public void update(Integer uid, ShippingForm form) {
		Shipping entity = new Shipping();
		BeanUtils.copyProperties(form, entity);
		entity.setUserId(uid);
		entity.setId(Integer.valueOf(form.getId()));
		this.updateById(entity);
	}

	@Override
	public PageInfo list(Integer uid, Integer pageNum, Integer pageSize) {
		QueryWrapper<Shipping> wrapper = new QueryWrapper();
		wrapper.in("user_id",uid);
		PageHelper.startPage(pageNum, pageSize);
		List<Shipping> shippingList = this.list(wrapper);
		PageInfo pageInfo = new PageInfo(shippingList);
		return pageInfo;
	}
}
